IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_WorkflowHistoryAdd]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_WorkflowHistoryAdd]
GO
create procedure [dbo].[cms_sp_WorkflowHistoryAdd]
@Id int = null output,
@WorkflowId uniqueidentifier, 
@UserGroupId int,
@UserId int = null,
@WorkflowState int,
@WorkflowData nvarchar(max),
@WorkflowDataType nvarchar(250),
@IsLatestState bit,
@Notes nvarchar(max) = null,
@CreatedBy int,
@DateCreated datetime,
@LastUpdatedBy int,
@DateLastUpdated datetime
as
if @IsLatestState = 1
begin
	update dbo.WorkflowHistory
		set IsLatestState = 0
	where WorkflowId = @WorkflowId
end
insert into dbo.WorkflowHistory
(
WorkflowId,
UserGroupId,
UserId,
WorkflowState,
WorkflowData,
WorkflowDataType,
IsLatestState,
Notes,
CreatedBy,
DateCreated
)
values
(
@WorkflowId,
@UserGroupId,
@UserId,
@WorkflowState,
@WorkflowData,
@WorkflowDataType,
@IsLatestState,
@Notes,
@CreatedBy,
@DateCreated
)
set @Id = SCOPE_IDENTITY()
return @Id


GO

